<?php
if( !defined( 'MEDIAWIKI' ) )
die(-1);

require_once('DatabaseFunctions.php');

#install extension hook
$wgHooks['BeforePageDisplay'][] = 'wfILikeThis';

function wfILikeThis(){
  global $wgRequest, $wgDBprefix, $wgOut, $wgUser, $wgTitle;

   
  // if type is setted DO THINGS
  if ($wgTitle->mNamespace != NS_ARTIST && $wgTitle->mNamespace != NS_SONG && $wgTitle->mNamespace !=NS_RELEASE) return true;
  $type = ($wgTitle->mNamespace - 100)/2 + 1;


  

  $out = '<div class="ilikethis">';  
  if ($wgUser->isLoggedIn()) {


      	$id_user = $wgUser->getId();
	if ($wgTitle->mNamespace==NS_ARTIST) 
	{
		$where = "artistKey = '{$wgTitle->mArtistKey}' and noartistKey is null";
	} else {
		$where = "artistKey = '{$wgTitle->mArtistKey}' and noartistKey = '{$wgTitle->mNoArtistKey}'";
	}
      	//query to know if this item its favotted already
      	$dbr = wfGetDB( DB_SLAVE );
      	$sql = "SELECT id FROM ss_ilikethis WHERE id_user=$id_user AND $where AND type = $type ";
      	$res = $dbr->query( $sql );
      	$row = $dbr->fetchObject( $res );

	$out .= '<span id="ilikethis_notice">&nbsp;</span>';
	if ($row !== false) {
      		$out .= "<a title='remove this from your faves' id='ilikethis_a' rowid='$row->id'
      		   href=\"javascript:ILikeThis($type,'{$wgTitle->mArtist}','{$wgTitle->mNoArtist}')\"><img id='heart' border='0' alt='' src='/skins/soundshare/heart_delete.png' /></a>";
	} else {
		$out .= "<a title='add this to your faves' id='ilikethis_a' rowid='0'
      		   href=\"javascript:ILikeThis($type,'{$wgTitle->mArtist}','{$wgTitle->mNoArtist}')\"><img id='heart' border='0' alt='' src='/skins/soundshare/heart_add.png' /></a>";
	}
	$dbr->freeResult( $res );
  } else {
	$out .= "<a title='login to add this to your faves' id='ilikethis_a' href='#'>
		   <img id='heart' border='0' alt='' src='/skins/soundshare/heart.png' /></a>";//if not logged paint grey heart
  }

  $out .= '</div>';

  $wgOut->setILikeThis($out);
  return true;
}




